<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserCoin;
use App\Models\UserCoinClue;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserCoinClueController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title('线索日志')
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid(request('user_id', 0)));
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($user_id)
    {
        return Grid::make((new UserCoinClue())->with(['coin', 'coinClue']), function (Grid $grid) use ($user_id) {
            $grid->model()->where('user_id', $user_id);
            $grid->column('id');
            $grid->column('coin.name', '硬币名称');
            $grid->column('coinClue.name', '线索名称');
            $grid->column('amount', '消耗橡果数');
            $grid->column('created_at', '解锁时间');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('coin.name', '硬币名称');
            });
            $grid->model()->orderBy('id', 'desc');

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserCoin(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('coin_id');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserCoin(), function (Form $form) {
            $form->display('id');
            $form->text('pay_status');
            $form->text('pay_time');
            $form->saving(function (Form $form) {
                $form->pay_time = '';
                if ($form->pay_status == \App\Models\UserCoin::PAY_STATUS_YES) {
                    $form->pay_time = date('Y-m-d H:i:s');
                }
            });
        });
    }
}
